package com.wm.blog_admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.controller.req.AdminReq;
import com.wm.blog_admin.query.TAdminQuery;
import com.wm.blog_admin.service.TAdminService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.context.AdminBaseContextHandler;
import com.wm.blog_common.domain.TAdminDO;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.entity.TAdmin;
import com.wm.blog_common.result.Result;
import com.wm.picture_interface.feign.PictureFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;


/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author 半卷流年
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/blog/admin")
public class TAdminController extends AdminBaseController {

    @Autowired
    private TAdminService tAdminService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TAdminDO> get(@PathVariable Long id) {
        return tAdminService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tAdminService.deleteById(id);
    }

    /**
     * 添加
     * @param tAdmin
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TAdmin tAdmin){
        return tAdminService.insert(tAdmin);
    }

    /**
     * 修改
     * @param tAdmin
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TAdmin tAdmin){
        return tAdminService.update(tAdmin);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TAdminDO>> page(Integer current, Integer size, TAdminQuery query){
        current = Optional.ofNullable(current).orElse(1);
        size = Optional.ofNullable(size).orElse(10);
        query = Optional.ofNullable(query).orElse(new TAdminQuery());
        return tAdminService.page(new QueryPage(current,size),query);
    }


    /**
     * 用户修改绑定角色
     * @param query
     * @return
     */
    @PostMapping("/bindRole")
    public Result bindRole(@RequestBody TAdminQuery query){
        return tAdminService.bindRole(query);
    }


    /**
     *   登录接口已集成到spring security中,配合gateway网关使用
     * @param query
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody TAdminQuery query){
        return tAdminService.login(query);
    }



    @Autowired
    private PictureFeignClient pictureFeignClient;

    /**
     *测试feign调用
     * @return
     */
    @GetMapping("/testFeign")
    public Result testFeign(){
        Result<TFileDO> result = pictureFeignClient.get(1L);
        return result;
    }


    /**
     *测试feign调用
     * @return
     */
    @GetMapping("/findByUserName")
    public Result findByUserName(String username){
        return tAdminService.findByUserName(username);
    }




    /**
     * 获取当前登录人的基本信息
     * @return
     */
    @GetMapping("/getCurrent")
    public Result getCurrent(){
       return tAdminService.getCurrent(AdminBaseContextHandler.getAdminUserId());
    }

    /**
     * 用户首页加载数据
     * @return
     */
    @GetMapping("/infoCardData")
    public Result infoCardData(){
        Long adminId = AdminBaseContextHandler.getAdminUserId();
        return tAdminService.infoCardData(adminId);
    }


    /**
     * 获取用户每周发布的博客数据展示
     * @return
     */
    @GetMapping("/weekActive")
    public Result weekActive(){
        return tAdminService.weekActive(AdminBaseContextHandler.getAdminUserId());
    }


    /**
     * 用户修改密码
     * @return
     */
    @PostMapping("/editPassword")
    public Result editPassword(@RequestBody TAdminQuery query, HttpServletRequest request){
        query = Optional.ofNullable(query).orElse(new TAdminQuery());
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        return tAdminService.editPassword(query);
    }


    /**
     * 新增账号
     * @return
     */
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody TAdminQuery query, HttpServletRequest request){
        query = Optional.ofNullable(query).orElse(new TAdminQuery());
        query.setAuthorization(super.currentAuthorization(request));
        return tAdminService.addAdmin(query,request);
    }

    /**
     * 停用账号
     * @return
     */
    @PutMapping("/disableAccount")
    public Result disAbleAccount(Long id){
        return tAdminService.disableAccount(id,AdminBaseContextHandler.getAdminUserId());
    }

    /**
     * 停用账号
     * @return
     */
    @GetMapping("/addSocket")
    public Result addSocket(String msg){
        return tAdminService.addSocket(msg);
    }


    /**
     * 新增账号
     * @return
     */
    @GetMapping("/testEmail")
    public Result testEmail(){
        try {
            return tAdminService.testEmail();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return Result.success();
    }


    /**
     * 账号激活
     * @param request
     * @param query
     * @return
     */
    @PutMapping("/activeAccount")
    public Result activeAccount( @RequestBody TAdminQuery query){
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        return tAdminService.activeAccount(query);
    }


    /**
     * 超级管理员激活账号
     * @param request
     * @return
     */
    @PutMapping("/adminActive")
    public Result adminActive(Long id){
        TAdminQuery query = new TAdminQuery();
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        query.setId(id);
        return tAdminService.adminActive(query);
    }



    /**
     * 导出excel
     * @return
     */
    @GetMapping("/exportAdmin")
    public void export(TAdminQuery query, HttpServletResponse response){
        tAdminService.export(query,response);
    }


    /**
     * 修改用户信息
     * @return
     */
    @PutMapping("/editAdmin")
    public Result editAdmin(@RequestBody AdminReq admin){
        return tAdminService.editAdmin(admin);
    }


}
